package com.cjm.cl;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;

public class Main {
    public static void main(String[] args) throws Exception {
        bbb();
    }

    private static void bbb() throws InstantiationException, IllegalAccessException {
        MyClassLoader myClassLoader = new MyClassLoader();
        myClassLoader.setRoot("D:\\tmp\\clazz");

        Class<?> testClass = null;
        try {
            testClass = myClassLoader.loadClass("com.cjm.cl.DemoTest");
            Object o = testClass.newInstance();
            System.err.println(o.getClass().getClassLoader());
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private static void abc() throws MalformedURLException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        URLClassLoader loader1 = new URLClassLoader(
                new URL[] {new File("D:\\tmp\\clazz").toURI().toURL()}
        );
        URLClassLoader loader2 = new URLClassLoader(
                new URL[] {new File("D:\\tmp\\clazz").toURI().toURL()}
        );
        ClassLoader loader3 = Main.class.getClassLoader();

        Class<?> demoTest = loader1.loadClass("com.cjm.cl.DemoTest");
        demoTest.newInstance();
        System.err.println("loader1=" + loader1);
//        System.err.println(o.getClass().getClassLoader());

        Class<?> demoTest2 = loader2.loadClass("com.cjm.cl.DemoTest");
        demoTest2.newInstance();
        System.err.println("loader2=" + loader2);
//        System.err.println(o1.getClass().getClassLoader());

        Class<?> demoTest3 = loader3.loadClass("com.cjm.cl.DemoTest");
        demoTest3.newInstance();
        System.err.println("loader3=" + loader3);
//        System.err.println(o2.getClass().getClassLoader());

        System.err.println("===========================1");

        // App ClassLoader
        System.out.println(new DemoTest().getClass().getClassLoader());
        // Ext ClassLoader
        System.out.println(new DemoTest().getClass().getClassLoader().getParent());
        // Bootstrap ClassLoader
        System.out.println(new DemoTest().getClass().getClassLoader().getParent().getParent());
        // Bootstrap ClassLoader
        System.out.println(new String().getClass().getClassLoader());
        System.err.println("=================================2");

        MyClassLoader classLoader = new MyClassLoader();
        Class<?> aClass = classLoader.loadClass(DemoTest.class.getName());
        System.err.println(aClass.getClassLoader());
    }
}
